﻿using SRB.Tg.Common;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class DrawingSteelTowerStructure
    {

        //BridgeStructureType bridge_type = BridgeStructureType.AviationBridge;
        string _type = "0";
        System.Data.DataTable dtProject = new System.Data.DataTable();
        System.Data.DataTable dtBridge = new System.Data.DataTable();
        System.Data.DataTable dtBridgeDrawing = new System.Data.DataTable();
        System.Data.DataTable dtProjectConfig = new System.Data.DataTable();

        string _bridgeId = null;
        DrawingConfigModel drawingConfigModel = new DrawingConfigModel();

        static double slopeW = 0.025;
        static double slopeL = 0.125 * 0.5;
        static List<double> pierHLst = new List<double>();
        static bool isStartLeft = true;
        static double scale = 200;
        static double light_distinct = 0;
        static double bridgeSlope = 0;
        static double bridgeLength = 0;


        public DrawingSteelTowerStructure(string bridgeId, string type,out System.Data.DataTable dtProjectTb,bool isMerge = false)
        {
            _type = type;
            #region  参数数据初始化
            dtBridgeDrawing = new System.Data.DataTable();//图纸目录信息表
            //传入bridgeId，查询出mainId
            string sqlMainId = "select * from busi_bridge where id='" + bridgeId + "' ";
            string mainId = MySqlHelper.ExecuteDataTable(sqlMainId, CommandType.Text).Rows[0]["main_id"].ToString();
            _bridgeId = mainId;
            string sqlBridge = "select * from busi_bridge_main where id='" + mainId + "'";
            dtBridge = MySqlHelper.ExecuteDataTable(sqlBridge, CommandType.Text);

            int bridge_scope = 1;//桥幅（单幅桥和双幅桥）
            if (dtBridge.Rows.Count > 0)
            {
                bridge_scope = int.Parse(dtBridge.Rows[0]["bridge_scope"].ToString());
                if (bridge_scope > 0)
                    isMerge = dtBridge.Rows[0]["drawing_level"].ToString() == "1" ? true : false; //双幅桥图纸是否合并
            }


            string sqlDrawing = "select * from busi_bridge_catalog where  bridge_main_id='" + mainId + "'";
            dtBridgeDrawing = MySqlHelper.ExecuteDataTable(sqlDrawing, CommandType.Text);
            #endregion

           

            dtProjectTb = dtBridgeDrawing;
        }

        public void DrawingSteelTowerCADByPier(Database db,  string bridgeId, string categoryId, System.Data.DataRow dr, int page, int count, out System.Data.DataTable dtProjectConfigTb, double bridgeW , double divCap, out int index, int startIndex = 0, string nameLast="")
        {
            index = 0;
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);
               string pierNo = dr["name"].ToString();

                //double divCap = 0, divHeight = 0;
                bridgeLength = 0;
                Line lineD = new Line();
                Line lineD0 = new Line();
         
                Point2d dPt1 = Point2d.Origin;
                List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();

                //bridgeSlope = double.Parse(dr["bridge_slope"].ToString()) * 0.01;

                //divCap = double.Parse(dr["beam_section_width"].ToString()) * 1000;
                //divHeight = double.Parse(dr["beam_height"].ToString()) * 1000;


                double pCH = double.Parse(dr["pile_top_height_correction"].ToString());
                double pierHeight = double.Parse(dr["pier_height"].ToString()) * 1000;
                double pierH = pierHeight - pCH * 1000;
                string steel_tower_type = dr["steel_tower_type"].ToString();
                slopeW = double.Parse(dr["slope_w"].ToString());
                slopeL = double.Parse(dr["slope_l"].ToString());


                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                double OverAllScale = drawingConfigModel.drawing_scale;
                //OverAllScale = 200;
                //if (pierHeight > 50 * 1000)
                //{
                //    drawingConfigModel.drawing_scale = drawingConfigModel.drawing_scale + 50;
                //    OverAllScale = drawingConfigModel.drawing_scale;
                //    InitConfig.InitDimStyle(db, Unit.Centimeter, Unit.Centimeter, OverAllScale, "SRB.HT");
                //    InitConfig.InitDimStyle(db, Unit.Millimeter, Unit.Centimeter, OverAllScale, "SRB.HT");
                //    InitConfig.InitDimStyle(db, Unit.Millimeter, Unit.Millimeter, OverAllScale, "SRB.HT");
                //    InitConfig.InitLeaderStyle(db, OverAllScale);
                //}
                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();
                string refPName = drawingConfigModel.name + "-P1";
                if(pierNo!="P1")
                    refPName = drawingConfigModel.name + "-"+nameLast;
         
                DrawingSteelTower.DrawingGemstoneShapeElevation(db, RefPt, ref Ext, pierH,  divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr,out index,startIndex, OverAllScale);
                double w = Ext.W();
                DrawingSteelTower.DrawingGemstoneShapeSide(db, RefPt.C2D(w+25*scale), ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, out index, startIndex, OverAllScale, refPName);

                drawingConfigModel.name = drawingConfigModel.name + "-" + pierNo ;


                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayoutNew(db, "图纸", Ext, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                //PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);
                PictureFrameTools.AddPaperDBText(db, tKPoint, 0, drawingConfigModel, ref ex, page, count);
                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }

        public void DrawingSteelTowerYSeg(Database db, int OverAllScale, string bridgeId, string categoryId, System.Data.DataRow dr,int page,int count, out System.Data.DataTable dtProjectConfigTb, double bridgeW = 3000,double bridgeSlope=0.025,double divCap=3000)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);
                string pierNo = dr["name"].ToString();

                //double divCap = 0, divHeight = 0;
                bridgeLength = 0;
                Line lineD = new Line();
                Line lineD0 = new Line();

                Point2d dPt1 = Point2d.Origin;
                List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();

                //bridgeSlope = double.Parse(dr["bridge_slope"].ToString()) * 0.01;

                //divCap = double.Parse(dr["beam_section_width"].ToString()) * 1000;
                //divHeight = double.Parse(dr["beam_height"].ToString()) * 1000;


                double pCH = double.Parse(dr["pile_top_height_correction"].ToString());
                double pierHeight = double.Parse(dr["pier_height"].ToString()) * 1000;
                double pierH = pierHeight - pCH * 1000;
                string steel_tower_type = dr["steel_tower_type"].ToString();
                slopeW = double.Parse(dr["slope_w"].ToString());
                slopeL = double.Parse(dr["slope_l"].ToString());
            

                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                OverAllScale = drawingConfigModel.drawing_scale;
                //OverAllScale = 200;
                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();
                
                    DrawingSteelTower.DrawingYGemstoneShapeElevation(db, RefPt, ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale);
                    double w = Ext.W();
                    DrawingSteelTower.DrawingYGemstoneShapeLeftSide(db, RefPt.C2D(w * 0.6+5* OverAllScale), ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale);
                    double w1 = Ext.W();
                    DrawingSteelTower.DrawingYGemstoneShapeRightSide(db, RefPt.C2D(w * 0.6 + (w1 - w) * 0.98 + 5 * OverAllScale), ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale);
                    RefPtNote = Point2d.Origin.C2D(); ;

                    drawingConfigModel.name = drawingConfigModel.name + "-P1";
                

                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayoutNew(db, "图纸", Ext, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint,0, drawingConfigModel, ref ex, page, count);
                drawingConfigModel.name = drawingConfigModel.name + "Y";
                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }


        public void DrawingSteelTowerByPier(Database db, string bridgeId, string categoryId, System.Data.DataRow dr, int page, int count, out System.Data.DataTable dtProjectConfigTb, double bridgeW, double divCap, out int index, int startIndex = 0, string nameLast = "")
        {
            index = 0;
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);
                string pierNo = dr["name"].ToString();

                //double divCap = 0, divHeight = 0;
                bridgeLength = 0;
                Line lineD = new Line();
                Line lineD0 = new Line();

                Point2d dPt1 = Point2d.Origin;
                List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();


                double pCH = double.Parse(dr["pile_top_height_correction"].ToString());
                double pierHeight = double.Parse(dr["pier_height"].ToString()) * 1000;
                double pierH = pierHeight - pCH * 1000;
                string steel_tower_type = dr["steel_tower_type"].ToString();
                slopeW = double.Parse(dr["slope_w"].ToString());
                slopeL = double.Parse(dr["slope_l"].ToString());


                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                double OverAllScale = drawingConfigModel.drawing_scale;
                if (pierH <= 20000)
                    OverAllScale = 100;
                else if (pierH <= 30000)
                    OverAllScale = 120;
                else
                    OverAllScale = drawingConfigModel.drawing_scale;
                Extents2d Ext, ExtNote;
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();
                string refPName = drawingConfigModel.name + "-P1";
                if (pierNo != "P1")
                    refPName = drawingConfigModel.name + "-" + nameLast;

                DrawingSteelTower.DrawingGemstoneShapeElevationRotate(db, RefPt, ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale);
                double w = Ext.W();
                DrawingSteelTower.DrawingGemstoneShapeSideRotate(db, RefPt.C2D(0, -Ext.L()*1.1), ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr,  OverAllScale, refPName);

                drawingConfigModel.name = drawingConfigModel.name + "-" + pierNo;


                ExtNote = new Extents2d();


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayoutRotate(db, "图纸", Ext, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                //PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);
                PictureFrameTools.AddPaperDBText(db, tKPoint, 0, drawingConfigModel, ref ex, page, count);
                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }

        public void DrawingSteelTowerByPierWithEngineeringQuantity(Database db, string bridgeId, string categoryId, System.Data.DataRow dr, int page, int count, out System.Data.DataTable dtProjectConfigTb, double bridgeW, double divCap, out int index, int startIndex = 0, string nameLast = "")
        {
            index = 0;
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);
                string pierNo = dr["name"].ToString();

                //double divCap = 0, divHeight = 0;
                bridgeLength = 0;
                Line lineD = new Line();
                Line lineD0 = new Line();

                Point2d dPt1 = Point2d.Origin;
                List<Tuple<double, double, double>> tupleLst = new List<Tuple<double, double, double>>();


                double pCH = double.Parse(dr["pile_top_height_correction"].ToString());
                double pierHeight = double.Parse(dr["pier_height"].ToString()) * 1000;
                double pierH = pierHeight - pCH * 1000;
                string steel_tower_type = dr["steel_tower_type"].ToString();
                slopeW = double.Parse(dr["slope_w"].ToString());
                slopeL = double.Parse(dr["slope_l"].ToString());


                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                int  strDrNo =int.Parse( Regex.Replace(drawingConfigModel.drawing_no, @"[^\d.\d]", ""));
                drawingConfigModel.drawing_no = drawingConfigModel.drawing_no.Replace(strDrNo.ToString("D2"), (strDrNo+page-1).ToString("D2"));
                double OverAllScale = drawingConfigModel.drawing_scale;
                if (pierH <= 20000)
                    OverAllScale = 100;
                else if (pierH <= 30000)
                    OverAllScale = 120;
                else
                    OverAllScale = drawingConfigModel.drawing_scale;
                Extents2d Ext ;
                Point2d RefPt;
                RefPt = Point2d.Origin;
                Ext = new Extents2d();
                string refPName = drawingConfigModel.name + "-P1";
                if (pierNo != "P1")
                    refPName = drawingConfigModel.name + "-" + nameLast;

                DrawingSteelTower.DrawingGemstoneShapeElevationRotate(db, RefPt, ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale);
                double w = Ext.W();
                DrawingSteelTower.DrawingGemstoneShapeSideRotate(db, RefPt.C2D(0, -Ext.L() * 1.1), ref Ext, pierH, divCap, bridgeW, slopeW, slopeL, bridgeSlope, dr, OverAllScale, refPName);

                drawingConfigModel.name = drawingConfigModel.name + "-" + pierNo;


                #endregion

                #region  材料表
                List<string> list = new List<string>();
                list.Add("材料");
                list.Add("材料编码");
                list.Add("规格");
                list.Add("单块重(kg)");
                list.Add("数量");
                list.Add("总重(kg)");
                list.Add("备注");


                //string pierNo = dr["name"].ToString();
                string spanId = dr["id"].ToString();
                string sqlEngineeringQuantity = "select *from (SELECT id, materiel, materiel_code, materiel_specs, unit_weight, unit,length,unit_weight*volume as unit_structural_weight,  sum(number) as number, SUM(volume) volume, SUM(total_weight) AS total_weight, remark  "
            + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel,materiel_code,materiel_specs, remark,length) a order by  materiel, materiel_code,remark asc ,number,materiel_specs,length";
                System.Data.DataTable dtEngineeringQuantity = MySqlHelper.ExecuteDataTable(sqlEngineeringQuantity, CommandType.Text);


                List<string> listM = new List<string>();
                listM.Add("材料");
                listM.Add("材料编码");
                listM.Add("规格");
                listM.Add("总重(kg)");
                listM.Add("小计(吨)");
                listM.Add("合计(吨)");
                //按规格汇总
                string sqlQuantity = "SELECT id, materiel, materiel_code, materiel_specs,SUM(total_weight) AS total_weight "
          + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel_code,materiel_specs,materiel";
                System.Data.DataTable dtQuantity = MySqlHelper.ExecuteDataTable(sqlQuantity, CommandType.Text);

         
                //drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                //OverAllScale = 1;

                Point2d  RefPtNote;
             
                List<Extents2d> exfLst = new List<Extents2d>();
                RefPtNote = Point2d.Origin.C2D(0,-Ext.L()*1.5); ;
                //DrawQuantityTable.DrawSpanMaterialsTable(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, list, Extensions.curFont, 1, pierNo + "墩材料汇总");
                DrawQuantityTable.DrawSpanMaterialsTableNew(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, dtQuantity, list, listM, Extensions.curFont, OverAllScale, pierNo + "墩材料表", pierNo + "墩材料汇总表");

                //drawingConfigModel.name = pierNo + "墩材料汇总表";



                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateSteelTowerLayout(db, "图纸", Ext, exfLst, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale, 1);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count+1);

                #endregion

                #region 模型空间到布局空间投影处理
                //LayoutTools.CreateLayoutRotate(db, "图纸", Ext, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale);
                //Point2d tKPoint = Point2d.Origin;

                //Extents2d ex = new Extents2d();
                ////PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);
                //PictureFrameTools.AddPaperDBText(db, tKPoint, 0, drawingConfigModel, ref ex, page, count);
                #endregion


            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }

        public void DrawingEngineeringQuantityTableByPier(Database db, int OverAllScale, string bridgeId, string categoryId, System.Data.DataRow dr, out System.Data.DataTable dtProjectConfigTb)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);

                List<string> list = new List<string>();
                list.Add("材料");
                list.Add("材料编码");
                list.Add("规格");
                list.Add("单块重(kg)");
                list.Add("数量");
                list.Add("总重(kg)");
                list.Add("备注");


                string pierNo = dr["name"].ToString();
                string spanId = dr["id"].ToString();
                string sqlEngineeringQuantity = "select *from (SELECT id, materiel, materiel_code, materiel_specs, unit_weight, unit,length,unit_weight*volume as unit_structural_weight,  sum(number) as number, SUM(volume) volume, SUM(total_weight) AS total_weight, remark  "
            + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel,materiel_code,materiel_specs, remark,length) a order by  materiel, materiel_code,remark asc ,number,materiel_specs,length";
                System.Data.DataTable dtEngineeringQuantity = MySqlHelper.ExecuteDataTable(sqlEngineeringQuantity, CommandType.Text);


                List<string> listM = new List<string>();
                listM.Add("材料");
                listM.Add("材料编码");
                listM.Add("规格");
                listM.Add("总重(kg)");
                listM.Add("小计(吨)");
                listM.Add("合计(吨)");
                //按规格汇总
                string sqlQuantity = "SELECT id, materiel, materiel_code, materiel_specs,SUM(total_weight) AS total_weight "
          + " FROM busi_engineering_quantity where bridge_id='" + bridgeId + "'and span_id='" + spanId + "'  GROUP BY materiel_code,materiel_specs,materiel";
                System.Data.DataTable dtQuantity = MySqlHelper.ExecuteDataTable(sqlQuantity, CommandType.Text);

                #region 绘制图形
                drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, categoryId, dtProjectConfig);
                OverAllScale = 1;
       
                Point2d RefPt, RefPtNote;
                RefPt = Point2d.Origin;
              

                List<Extents2d> exfLst = new List<Extents2d>();
                RefPtNote = Point2d.Origin.C2D(); ;
                //DrawQuantityTable.DrawSpanMaterialsTable(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, list, Extensions.curFont, 1, pierNo + "墩材料汇总");
                DrawQuantityTable.DrawSpanMaterialsTableNew(db, ref exfLst, RefPtNote.C3D(), dtEngineeringQuantity, dtQuantity, list, listM, Extensions.curFont, 1, pierNo + "墩材料表", pierNo + "墩材料汇总表");

                drawingConfigModel.name = pierNo + "墩材料汇总表";
             


                #endregion

                #region 模型空间到布局空间投影处理
                LayoutTools.CreateLayout(db, "图纸", exfLst, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale, 1);
                Point2d tKPoint = Point2d.Origin;

                Extents2d ex = new Extents2d();
                PictureFrameTools.AddPaperDBText(db, tKPoint, drawingConfigModel, ref ex, exfLst.Count);

                #endregion


            }
            catch (System.Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }

    }


}
